<script setup lang="ts" name="UserEdit">
const { proxy } = getCurrentInstance();

const { sys_user_sex, sys_show_hide, sys_user_interest } = useDict("sys_user_sex", "sys_show_hide", "sys_user_interest");

const emit = defineEmits(['closeDialog']);

const props = defineProps({
  id: {
    type: Number,
    default: null,
  },
  title: {
    type: String,
    default: false,
  }
});

// 用户表单
const userFormRef = ref(null);

// 弹窗开关
const open = ref(false);

// 表单数据
const form = ref<UserRecord>({});

// 表单校验
const rules = ref({
  userId: [
    { required: true, message: "ID不能为空", trigger: "blur" }
  ],
  userName: [
    { required: true, message: "名字不能为空", trigger: "blur" }
  ],
  userAge: [
    { required: true, message: "年龄不能为空", trigger: "blur" }
  ],
  userSex: [
    { required: true, message: "性别不能为空", trigger: "change" }
  ],
  userImg: [
    { required: true, message: "头像不能为空", trigger: "change" }
  ],
  userInterest: [
    { required: true, message: "爱好不能为空", trigger: "change" }
  ],
  userBirthdayTime: [
    { required: true, message: "生日不能为空", trigger: "change" }
  ],
  userDescribe: [
    { required: true, message: "描述不能为空", trigger: "blur" }
  ],
});

// 加载
const loading = ref(false);

/** 获取用户详情 */
function getData() {
  reset();
  if (isEmpty(props.id)) {
    open.value = true;
  } else {
    getUserApi(props.id).then(response => {
      form.value = response.data;
      open.value = true;
    }).catch(() => {
      closeDialog(false);
    })
  }
}

/** 表单重置 */
function reset() {
  form.value = {
    userId: null,
    userName: null,
    userAge: null,
    userSex: null,
    userImg: null,
    userInterest: [],
    userBirthdayTime: null,
    userDescribe: null,
  };
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  open.value = false;
  emit("closeDialog", refresh);
}

/** 提交按钮 */
function submitForm() {
  // @ts-ignore
  userFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      if (isEmpty(form.value.userId)) {
        addUserApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("新增成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false;
        });
      } else {
        updateUserApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("修改成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false;
        });
      }
    }
  });
}

watch(open, (val) => {
  if (!val) {
    closeDialog(false);
  }
});

getData();
</script>

<template>
  <!-- 用户编辑对话框 -->
  <el-dialog :title="title" v-model="open" width="700px" append-to-body :close-on-click-modal="false">
    <el-form ref="userFormRef" :model="form" :rules="rules" :inline="true" label-width="80px">
      <el-form-item label="ID" prop="userId">
        <el-input v-model="form.userId" placeholder="请输入ID" />
      </el-form-item>
      <el-form-item label="名字" prop="userName">
        <el-input v-model="form.userName" placeholder="请输入名字" />
      </el-form-item>
      <el-form-item label="年龄" prop="userAge">
        <el-input-number v-model="form.userAge" :min="1" placeholder="请输入年龄" />
      </el-form-item>
      <el-form-item label="性别" prop="userSex" class="item-describe">
        <el-radio-group v-model="form.userSex">
          <el-radio
              v-for="item in sys_user_sex"
              :key="item.value"
              :value="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="头像" prop="userImg">
        <el-select v-model="form.userImg" placeholder="请选择头像">
          <el-option
              v-for="item in sys_show_hide"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="爱好" prop="userInterest" class="item-describe">
        <el-checkbox-group v-model="form.userInterest">
          <el-checkbox
              v-for="item in sys_user_interest"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="生日" prop="userBirthdayTime">
        <el-date-picker
            v-model="form.userBirthdayTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择生日"
        />
      </el-form-item>
      <el-form-item label="描述" prop="userDescribe" class="item-describe">
        <el-input v-model="form.userDescribe" type="textarea" placeholder="请输入描述" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" v-debounce="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>